package com.consumer.config;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description
 * @Author youxin
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2021/11/16
 */
@Configuration
public class ConsumerConfiguration {

    @Value("${rocketmq.login.group.consumer}")
    private String loginGroupConsumer;
    @Value("${rocketmq.namesrv.address}")
    private String namesrvAddr;
    @Value("${rocketmq.login.topic}")
    private String loginSendTopic;

    @Bean(value = "loginConsumer")
    public DefaultMQPushConsumer loginConsumer(@Qualifier(value = "loginMessageLinstener") LoginMessageLinstener loginMessageLinstener) throws MQClientException {
        DefaultMQPushConsumer defaultMQPushConsumer=new DefaultMQPushConsumer(loginGroupConsumer);
        defaultMQPushConsumer.setNamesrvAddr(namesrvAddr);
        //TODO 比如：Consumer.subscribe（"TopicTest"，"tag1||tag2||tag3"），表示这个 Consumer 要消费“TopicTest”下带有tag1或tag2或tag3的消息
        defaultMQPushConsumer.subscribe(loginSendTopic,"*");
        defaultMQPushConsumer.registerMessageListener(loginMessageLinstener);
        defaultMQPushConsumer.start();
        return defaultMQPushConsumer;
    }
}
